package main

import (
	"backend/pkg/logging"
	"backend/pkg/setting"
	"backend/routers"
	"fmt"
	"net/http"
	"os"
	"os/signal"
)

func main() {
	setting.Setup()
	logging.Setup()

	logging.Info("Server starting")
	router := routers.InitRouter()
	server := &http.Server{
		Addr:    fmt.Sprintf(":%d", setting.ServerSetting.HttpPort),
		Handler: router,
	}

	quit := make(chan os.Signal)
	signal.Notify(quit, os.Interrupt)

	go func() {
		<-quit
		logging.Info("receive interrupt signal")
		if err := server.Close(); err != nil {
			logging.Fatal("Server Close:", err)
		}
	}()

	if err := server.ListenAndServe(); err != nil {
		if err == http.ErrServerClosed {
			logging.Info("Server closed under request")
		} else {
			logging.Fatal("Server closed unexpect")
		}
	}

	logging.Info("Server exiting")
}
